package com.sumainfor.wx.tenant.service.impl;

import com.sumainfor.common.entity.WxUserEntity;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.common.wechat.WeChatConfig;
import com.sumainfor.wx.tenant.dao.TctenTenantCouponsMapper;
import com.sumainfor.wx.tenant.feign.MarketFeignClient;
import com.sumainfor.wx.tenant.feign.OperationsFeignClient;
import com.sumainfor.wx.tenant.feign.ZuulFeignClient;
import com.sumainfor.wx.tenant.service.SignedService;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-02-25 17:55
 * 合同签约
 */
@Service
public class SignedServiceImpl implements SignedService {

    Logger log= LoggerFactory.getLogger(SignedServiceImpl.class);

    @Autowired
    private MarketFeignClient marketFeignClient;

    @Autowired
    TctenTenantCouponsMapper TctenTenantCouponsMapper;

    @Autowired
    OperationsFeignClient OperationsFeignClient;

    @Autowired
    ZuulFeignClient ZuulFeignClient;

    /**
     * 消息通知
     *
     * @param params
     * @return
     */
    @Override
    public JsonResult noticeStateResult(Map<String, Object> params) {
        log.info("合同签约回调：消息通知 参数 =====> {}", params);
        return marketFeignClient.noticeStateResult(params);
    }

    /**
     * 查询当前登录的用户等待签约的列表
     * @return
     */
    @Override
    public List<Map<String,Object>> getWaitSignList(Map<String,Object>params) {
        List<Map<String,Object>>result=new ArrayList<Map<String,Object>>();
        Map<String,Object>getWaitSignMap=(Map<String,Object>)marketFeignClient.getWaitSignList(params);
        log.info("getWaitSignMap---------"+getWaitSignMap);
        if(getWaitSignMap.get("errorCode").equals("0")||getWaitSignMap.get("errorCode").toString()=="0"){
            result=(List<Map<String,Object>>)marketFeignClient.getWaitSignList(params).getResult();
        }
        return result;
    }

    /**
     * 功能描述:
     * 根据合同编号获取合同状态
     * FlowStatus 电子合同签署状态  \n1.签署中\n2:签署完成\n3:失败\n4:拒签
     * IfFlowPay  电子合同是否支付     0无状态   1已完成未支付    2已完成已支付（该状态只有电子合同状态为2才有用）
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/20 10:32
     */
    public JsonResult getPactStatus(Map<String,Object>params){
        JsonResult<Map<String, Object>> jsonResult = marketFeignClient.getPactStatus(params);
        if (jsonResult.isSuccess() == false) {
            return jsonResult;
        }
        return JsonResult.success(jsonResult.getResult());
    }

    /**
     * 查询签约流程第一个流程的数据
     * @return
     */
    public JsonResult getPactSignFlowOne(Map<String,Object>params) {
        JsonResult<Map<String, Object>> jsonResult = marketFeignClient.getPactSignFlowOne(params);
        if (jsonResult.isSuccess() == false) {
            return jsonResult;
        }
        return JsonResult.success(jsonResult.getResult());
    }

    /**
     * 查询签约流程第二步数据;
     *
     * @param pactId
     * @return
     */
    @Override
    public JsonResult getPactSignFlowTwo(String pactId) {
        return marketFeignClient.getPactSignFlowTwo(pactId);
    }

    /**
     * 查询签约流程第三步和第四步数据;
     *
     * @param pactId
     * @return
     */
    @Override
    public JsonResult getPactSignOneThreeAndFour(String pactId) {
        return marketFeignClient.getPactSignThreeAndFour(pactId);
    }

    /**
     * 查询签约流程第五步数据;
     *
     * @param pactId
     * @return
     */
    @Override
    public JsonResult getPactSignFive(String pactId) {
        return marketFeignClient.getPactSignFive(pactId);
    }

    /**
     * 功能描述: 
     * 根据合同编号获取合同第八步资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/20 14:28
     */
    @Override
    public Map<String,Object> getPactSignEight(Map<String, Object> params) {
        Map<String,Object>getPactSignEight=new HashMap<>();
        JsonResult<Map<String, Object>> jsonResult = marketFeignClient.getPactSignEight(params);
        if (jsonResult.isSuccess() == true) {
            getPactSignEight=jsonResult.getResult();
            params.put("State",0);
            //获取用户最大优惠券
            Map<String,Object>getTenantCouponsMaxMap=TctenTenantCouponsMapper.getTenantCouponsMaxMap(params);
            getPactSignEight.put("Coupons",getTenantCouponsMaxMap);
        }else{
            getPactSignEight.put("Coupons",null);
        }

        JsonResult<Map<String, Object>> jsonResult1 = OperationsFeignClient.getBillMonRentList(params);
        if (jsonResult1.isSuccess() == true) {
            Map<String,Object>getBillMonRentList=jsonResult1.getResult();
            getPactSignEight.putAll(getBillMonRentList);
        }else {
            getPactSignEight.put("WechatStatus","1");
            getPactSignEight.put("PayStatus","1");
        }

        JsonResult<Map<String, Object>> jsonResult2 = ZuulFeignClient.getAreaPayMap(getPactSignEight);
        if (jsonResult2.isSuccess() == true) {
            Map<String,Object>getAreaPayMap=jsonResult2.getResult();
            getPactSignEight.put("Account",getAreaPayMap.get("Account"));
            getPactSignEight.put("AccountFile",getAreaPayMap.get("AccountFile"));
        }else {
            getPactSignEight.put("Account", WeChatConfig.DEFAULT_PAY_ACCONUNT);
            getPactSignEight.put("AccountFile",WeChatConfig.DEFAULT_PAY_ACCONUNTFILE);
        }

        return  getPactSignEight;
    }

    /**
     * 功能描述: 
     * 根据租客编号获取租客合同列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/23 12:22
     */
    public JsonResult getPactList(Map<String, Object> params,Pager pager) {
        JsonResult<Map<String, Object>> jsonResult = marketFeignClient.getPactList(params,pager);
        if (jsonResult.isSuccess() == false) {
            return jsonResult;
        }
        return JsonResult.success(jsonResult.getResult());
    }
}
